package com.ysxsoft.wdg.ui.activity.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.StOrderStatusDetailResponse;
import com.ysxsoft.wdg.bean.response.StPayResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.widget.alertview.AlertUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

public class StOrderStatusDetailActivity extends BaseActivity {
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.title_tv_r)
    TextView titleTvR;
    @BindView(R.id.iv_goods)
    ImageView ivGoods;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.tv_rules)
    TextView tvRules;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_goods_money)
    TextView tvGoodsMoney;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.code)
    TextView code;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    private String orderId;

    public static void start(Context context, String orderId) {
        Intent intent = new Intent(context, StOrderStatusDetailActivity.class);
        intent.putExtra("orderId", orderId);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_st_order_status_detail;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("实体订单");

        orderId = getIntent().getStringExtra("orderId");

        getData();
    }

    private void fillView(StOrderStatusDetailResponse bean) {
        StOrderStatusDetailResponse.DataBean dataBean = bean.getData();

        //商品信息
        List<StOrderStatusDetailResponse.DataBean.DetailBean> product=dataBean.getDetail();
        StOrderStatusDetailResponse.DataBean.DetailBean d=product.get(0);

        Glide.with(this).load(d.getPic()).into(ivGoods);


        tvGoodsName.setText(d.getProduct_name());
        tvMoney.setText("￥" + d.getAccount());
        tvNum.setText(d.getNum() + "");

        tvGoodsMoney.setText("￥" + dataBean.getAccount());//总价
        code.setText("取货码：" + dataBean.getCode());

        tvOrderNo.setText("订单编号：" + dataBean.getDsn());//订单编号
        tvOrderTime.setText("订单时间：" + dataBean.getAddtime());//订单时间
        tvPayType.setText("支付方式：" + "在线支付");//支付方式
        tvPayTime.setText("支付时间：" + dataBean.getPaytime());//支付时间
    }

    @Override
    protected void setListener() {
        titleFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 自营商城余额支付
     */
    private void getData() {
        OkHttpUtils.post()
                .url(UrlHost.ST_DETAIL)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("orderid", orderId)//订单id
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    StOrderStatusDetailResponse stPayResponse = JsonUtils.parseByGson(response, StOrderStatusDetailResponse.class);
                    if (stPayResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(stPayResponse.getCode())) {
                        fillView(stPayResponse);
                    } else {
                        ToastUtils.showToast(stPayResponse.getMs());
                    }
                } else {
                }
            }
        });
    }
}
